using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
using System.Xml.XPath;
using System.Collections;


//AUI Model Component
using AdaptiveHelpDesk.AUI_Model_Components.Watcher.TextBox;

namespace AdaptiveHelpDesk
{
    class UpdateUserModelTextBox
    {

        #region VARIABLES

        SqlConnection con = null;
        SqlDataReader rdr = null;
        XmlDocument doc;

        XmlElement InteractionFeatures;

        XmlElement KeyInteraction;

        String UserID = "";
        String dateLogged = "";
        String timeLogged = "";


        KeystrokeInteraction classKeystrokeInteraction;

        String getStoredProcedureColumn = "";
        String getStoreProcedureName = "";
        String updateStoredProcedureColumn = "";
        String updateStoreProcedureName = "";

        #endregion



        #region INITIALISATION

            public UpdateUserModelTextBox(string UID, string dateL, string timeL, string getSPName, string getSPColumn, string updateSPName, string updateSPColumn)
            {

                UserID = UID;
                dateLogged = dateL;
                timeLogged = timeL;
                getStoredProcedureColumn = getSPColumn;
                getStoreProcedureName = getSPName;
                updateStoredProcedureColumn = updateSPColumn;
                updateStoreProcedureName = updateSPName;

                //Get the User Model for the specified INformative Moment
                this.getUserModelTextBoxXML(UID, dateL, timeL);

                classKeystrokeInteraction = new KeystrokeInteraction();

                InteractionFeatures = (XmlElement)doc.DocumentElement.FirstChild;

                KeystrokeInteraction();

            }

        #endregion



        #region OPEN CONNECTION

            private void openConnection()
            {
                con = new SqlConnection("Data Source=" + System.Environment.MachineName + ";Initial Catalog=ISD;Integrated Security=True");
                //con = new SqlConnection("Data Source=ROCABEE;Initial Catalog=ISD;Integrated Security=True");
                con.Open();
            }

        #endregion



        #region GET USER MODEL XML SO THAT IT CAN BE UPDATED

            private XmlDocument getUserModelTextBoxXML(string UID, string dateLog, string timeLog)
            {
                try
                {
                    openConnection();
                    SqlCommand cmd = new SqlCommand(getStoreProcedureName, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@userID", UID));
                    cmd.Parameters.Add(new SqlParameter("@date", dateLog));
                    cmd.Parameters.Add(new SqlParameter("@time", timeLog));

                    rdr = cmd.ExecuteReader();
                    doc = new XmlDocument();
                    String XMLstring = "";

                    while (rdr.Read())
                    {
                        XMLstring = (String)rdr[getStoredProcedureColumn];
                    }

                    doc.LoadXml(XMLstring);

                    return doc;
                }
                finally
                {
                    if (con != null)
                    {
                        con.Close();
                    }
                    if (rdr != null)
                    {
                        rdr.Close();
                    }
                }
            }

        #endregion



        #region UPDATE USER MODEL XML

            public void updateUserModelTextBoxXML()
            {
                try
                {
                    openConnection();
                    SqlCommand cmd = new SqlCommand(updateStoreProcedureName, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@userID", UserID));
                    cmd.Parameters.Add(new SqlParameter("@date", dateLogged));
                    cmd.Parameters.Add(new SqlParameter("@time", timeLogged));
                    cmd.Parameters.Add(new SqlParameter(updateStoredProcedureColumn, doc.InnerXml));

                    cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (con != null)
                    {
                        con.Close();
                    }
                    if (rdr != null)
                    {
                        rdr.Close();
                    }
                }
            }

        #endregion



       #region UPDATE KEYSTROKE INTERACTION FEATURES

        private void KeystrokeInteraction()
        {
            KeyInteraction = (XmlElement)InteractionFeatures.GetElementsByTagName("KeystrokeInteraction")[0];
        }

            public void updateWPM(int wordCount, int totalTextTime)
            {
                KeyInteraction.ChildNodes[0].InnerText = classKeystrokeInteraction.getWPM(wordCount, totalTextTime).ToString();
                this.updateUserModelTextBoxXML();
            }

            //public void updateNrErrors(string text,RichTextBox rTextBox, NetSpell.SpellChecker.Spelling spelling)
            //{
            //    KeyInteraction.ChildNodes[1].InnerText = classKeystrokeInteraction.getNrErrors(text, rTextBox, spelling).ToString();
            //    this.updateUserModelTextBoxXML();
            //}

       #endregion


    }
}
